package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.comm.entity.News;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ClassMsg;
import cn.stylefeng.guns.modular.school.entity.StudentGuardian;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ClassMsgService;
import cn.stylefeng.guns.modular.school.service.StudentGuardianService;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 轮播图控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/student")
public class ApiNeedTokenStudentController extends BaseController {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentGuardianService studentGuardianService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private ClassMsgService classMsgService;
    @Autowired
    private GunsProperties gunsProperties;

    /**
     * 根据code找学生
     */
    @RequestMapping(value = "/code")
    public Object findStudentByCode(String studentCode, String tel) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();
        // 学生信息
        StudentInfo studentInfo = studentInfoService.findByCode(studentCode);
        if (studentInfo == null) {
            return new ErrorResponseData(500, "学号错误！");
        }
        // 监护人信息
        StudentGuardian studentGuardian = studentGuardianService.findByStudentIdCode(studentInfo.getId(), tel);
        if (studentGuardian == null) {
            return new ErrorResponseData(500, "学号和账户不匹配！");
        }
        // 班级信息
        ClassInfo classInfo = classInfoService.getById(studentInfo.getClassId());
        FileInfo fileInfo = fileInfoService.getById(classInfo.getFileId());
        String substring = fileInfo.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/");
        // 班级置顶消息
        ClassMsg classMsg = classMsgService.findTopOne(classInfo.getId());

        map.put("studentInfo", studentInfo);
        map.put("classInfo", classInfo);
        map.put("fileInfoPath", substring);
        map.put("classMsg", classMsg);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


